package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.SignalLampEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by pjn on 2017/7/22.
 */
public interface SignalLampRepository extends CrudRepository<SignalLampEntity, Integer> {


    /**
     * @Description: 信号灯产品dt展示
     * @Author: xu_jiaHui
     * @Date: 2017/7/25 15:16
     */
    public List<SignalLampDto> getSignalLamp4DT(DTRequestDto dtRequestDto,
                                                String actorIds,
                                                Integer orgId,
                                                Boolean audit,
                                                String startRoad,
                                                String endRoad,
                                                Integer alarmBinding) throws Exception;

    /**
     * @Description: 信号灯产品dt数量
     * @Author: xu_jiaHui
     * @Date: 2017/7/25 15:29
     */
    public Integer getSignalLamp4DTCount(DTRequestDto dtRequestDto,
                                         String actorIds,
                                         Integer orgId,
                                         Boolean audit,
                                         String startRoad,
                                         String endRoad,
                                         Integer alarmBinding) throws Exception;

    /**
     * @Description: 获取第一个路名
     * @Author: pan-jianan
     * @Date: 2017/7/23 0:41
     */
    public List<String> getStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 根据第一个获取第二个路名
     * @Author: pan-jianan
     * @Date: 2017/7/23 0:41
     */
    public List<String> getSectionByStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 删除信号灯
     * @Author: xu_jiaHui
     * @Date: 2017/7/26 13:05
     */
    @Modifying
    @Query(value = "delete from SignalLampEntity where signalLampId=?1")
    public Integer deleteSignalLamp(Integer signalMachId) throws Exception;

    /**
     * @Description: 编辑信号灯
     * @Author: xu_jiaHui
     * @Date: 2017/7/26 13:40
     */
    @Modifying
    @Query(value = "update SignalLampEntity set projectNumber=?2,signalLampRegionId=?3,signalLampLongitude=?4," +
            "signalLampLatitude=?5,signalLampProvince=?6,signalLampCity=?7,signalLampArea=?8,signalLampStartRoad=?9," +
            "signalLampEndRoad=?10,signalLampType=?11,signalLampBrand=?12,signalLampPhoto=?13,signalLampInstallTime=?14," +
            "signalLampModifierId=?15,signalLampPeriod=?16,signalLampServiceLife=?17,signalLampRemark=?18," +
            "signalLampInstallOrg=?19,signalLampMainOrgId=?20,signalLampIsComplete=?21 where  signalLampId=?1")
    public Integer updateSignalLamp(Integer signalLampId,
                                    String projectNumber,
                                    Integer signalLampRegionId,
                                    String signalLampLongitude,
                                    String signalLampLatitude,
                                    String signalLampProvince,
                                    String signalLampCity,
                                    String signalLampArea,
                                    String signalLampStartRoad,
                                    String signalLampEndRoad,
                                    String signalLampType,
                                    String signalLampBrand,
                                    String signalLampPhoto,
                                    String signalLampInstallTime,
                                    Integer signalLampModifierId,
                                    Integer signalLampPeriod,
                                    Integer signalLampServiceLife,
                                    String signalLampRemark,
                                    String signalLampInstallOrg,
                                    Integer signalLampMainOrgId,
                                    Integer signalLampIsComplete) throws Exception;

    /**
     * @Description: 获取信号灯坐标用于地图显示
     * @Author: xu_jiaHui
     * @Date: 2017/7/27 13:41
     */
    public List<SignalLampDto> getSignalLampCoordinate4Map(Integer orgId, Integer orgType, Integer actorId) throws Exception;


    /**
     * @Description: 信号灯信息展示4app
     * @Author: xu_jiaHui
     * @Date: 2017/7/27 15:31
     */
    public List<SignalLampDto> getSignalLamp4APP(
            String actorIds, Integer orgId, Integer mCount,
            Integer mSize, String city, String startRoad, String endRoad, String alarmType) throws Exception;


    /**
     * @Description: 通过id获取信号灯信息
     * @Author: xu_jiaHui
     * @Date: 2017/7/27 15:32
     */
    public List<SignalLampDto> getSignalLampById(Integer signalLampById) throws Exception;

    /**
     * @Description: 通过坐标获取信号灯信息
     * @author xu_jiaHui
     * @date 2017/7/29 15:31
     */
    @Query(value = "select new com.hualude.roadsign.dto.MapInfoDto(sm.signalLampId,sm.signalLampPhoto,sm.projectNumber," +
            "sm.signalLampProvince,sm.signalLampCity,sm.signalLampArea,sm.signalLampStartRoad," +
            "sm.signalLampEndRoad,sm.signalLampType,sm.signalLampBrand,sm.signalLampInstallTime,sm.signalLampInstallOrg,sm.signalLampPeriod," +
            "sm.signalLampServiceLife,sm.signalLampRemark,u.userName,o1.orgName,o2.orgName) from " +
            "SignalLampEntity sm,OrgEntity o1,OrgEntity o2,UserEntity u where o1.orgId=sm.signalLampOrgId and " +
            "o2.orgId=sm.signalLampMainOrgId and sm.signalLampCreatorId=u.userId and sm.signalLampLongitude=?1 " +
            "and sm.signalLampLatitude=?2")
    public MapInfoDto getSignalLampInfoByXY(String solarEnergyLongitude, String solarEnergyLatitude) throws Exception;


    /**
     * @Description: 根据OrgId，actorId获取城市4App
     * @author xu_jiaHui
     * @date 2017/7/30 15:17
     */
    public List<String> getCity4SignalLamp(Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception;

    /**
     * @Description: 根据city获取路口
     * @author xu_jiaHui
     * @date 2017/7/30 15:24
     */
    public List<String> getStartRoadByCity(String city, Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception;

    /**
     * @Description: 获取信号灯信息用于App地图展示
     * @author xu_jiaHui
     * @date 2017/8/3 14:11
     */
    public List<SignalLampDto> getSignalLamp4Map4App(Integer orgId,
                                                     String actorIds,
                                                     double neLongitude,
                                                     double neLatitude,
                                                     double swLongitude,
                                                     double swLatitude) throws Exception;

    /**
     * @Description: 根据第一个获取第二个路名4App
     * @Author: pan-jianan
     * @Date: 2017/7/23 0:41
     */
    public List<String> getSectionByStartRoad4App(String roadName, String city, Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception;

    /**
     * @Description: 获取信号灯坐标4MAP
     * @Author: pan-jianan
     * @date 2017/8/3 14:11
     */
    public List<SignalLampFaultDto> getLampCoordinate4Map(Integer orgId, Integer actorId) throws Exception;

    /**
     * @Description: 根据坐标获取信号灯信息
     * @Author: pan-jianan
     * @date 2017/8/3 14:11
     */
    public List<SignalLampFaultDto> getLampEnergyByXY(String longitude, String latitude) throws Exception;

    public List<SignalLampFaultDto> getLampFaultReport4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId, String signalLampStartRoad, String signalLampEndRoad) throws Exception;

    /**
     * @Description: 审核信号灯
     * @Author: xu_jiaHui
     * @Date: 2017/8/13 13:51
     */
    @Modifying
    @Query(value = "update SignalLampEntity set signalLampIsComplete=1 where signalLampId=?1")
    public Integer auditSignalLamp(Integer signalLampId) throws Exception;

    /**
     * @Description: 绑定报警器设备
     * @Author: WYX.
     * @Date: 2018/4/30 23:16.
     */
    @Modifying
    @Query(value = "update SignalLampEntity set alarmDeviceId=?1 where signalLampId=?2 and alarmDeviceId=null")
    public Integer bindingDevice(String deviceId, Integer signalLampId) throws Exception;

    /**
     * @Description: 解绑报警器设备
     * @Author: WYX.
     * @Date: 2018/4/30 23:19.
     */
    @Modifying
    @Query(value = "update SignalLampEntity set alarmDeviceId=null where signalLampId=?1")
    public Integer unbindingDevice(Integer signalLampId) throws Exception;

    public List<SignalLampDto> getDeviceInfo(Integer deviceId) throws Exception;

}
